package com.tg.app.activity.device.add;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import com.base.BaseActivity;
import com.tg.app.R;
import com.tg.app.fragment.BlurDialogFragment;
import com.tg.app.helper.ActivityHelper;

public class SelectDeviceActivity extends BaseActivity {
    private static final String TAG = SelectDeviceActivity.class.getSimpleName();
    private static final int UUID_LENGTH = 12;
    private Button mButtonView;
    private EditText editText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_device_type);
        hideActionBar();
        initView();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    private void showPopupWindow() {
        BlurDialogFragment fragment = BlurDialogFragment.newInstance();
        fragment.setBlurDialogListener(view -> {
            mButtonView = view.findViewById(R.id.btn_scan_qrcode_next);
            ActivityHelper.setButtonEnable(mButtonView, false);
            editText = view.findViewById(R.id.edt_scan_qrcode_uuid);
            editText.setTextIsSelectable(true);

            mButtonView.setOnClickListener(v -> {
                AddDeviceReportHelper.onEventClickByName(TAG,"btn_scan_qrcode_next");
                bindDevice();
            });

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
                @Override
                public void afterTextChanged(Editable editable) {
                    String s = editable.toString();
                    ActivityHelper.setButtonEnable(mButtonView, s.length() == UUID_LENGTH);
                }
            });
        });
        fragment.show(getFragmentManager(), SelectDeviceActivity.class.getSimpleName());
    }


    public void initView() {
        modifyToolBar(R.string.txt_scan_no_result);
        setClickBack(null);
        findViewById(R.id.rl_new_device).setOnClickListener(v -> ActivityHelper.goToSearchWifi(SelectDeviceActivity.this));
        findViewById(R.id.tv_textview).setOnClickListener(v ->  showPopupWindow());
        findViewById(R.id.rel_device_info_id).setOnClickListener(v -> ActivityHelper.goToBindHistroyDeviceListActivity(SelectDeviceActivity.this));
    }

    private void bindDevice() {
        final String uuid = editText.getText().toString().trim();
        ActivityHelper.goToBindActivity(SelectDeviceActivity.this, uuid, ScanQrcodeActivity.EXT_FROM_HISTROY);
    }
}
